const httpUtils = require('../../../utils/httpUtils')
const ui = require('../../../utils/ui')
//微信小程序AppSecret: af5aa3c973352384aacc43d6e26b6a49
Page({
  /**
   * 页面的初始数据
   */
  data: {
    current: 1,
    loginId: '',
    password: '',
    registLoginId: '',
    registNickName: '',
    registPassword: '',
    confirmPassword: ''
  },
  // 登陆注册监听
  click(e) {
    let index = e.currentTarget.dataset.code;
    this.setData({
      current: index,
      loginId: '',
      password: '',
      registLoginId: '',
      registNickName: '',
      registPassword: '',
      confirmPassword: ''
    })
  },
  //登录用户名
  userNameInput: function (e) {
    this.setData({
      loginId: e.detail.value
    })
  },
  //登录密码
  passWordInput: function (e) {
    this.setData({
      password: e.detail.value
    })
  },
  //注册登陆号
  registLoginIdInput: function (e) {
    this.setData({
      registLoginId: e.detail.value
    })
  },
  //注册昵称
  registNickNameInput: function (e) {
    this.setData({
      registNickName: e.detail.value
    })
  },
  //注册密码
  registPasswordInput: function (e) {
    this.setData({
      registPassword: e.detail.value
    })
  },
  //注册确认密码
  confirmPasswordInput: function (e) {
    this.setData({
      confirmPassword: e.detail.value
    })
  },

  confirm(e) {
    let pageCode = this.data.current;
    if (0 == pageCode) {
      console.log('注册Tab点击了确定')
      this.userRegist();
    } else {
      console.log('登录Tab点击了确定')
      this.userLogin();
    }
  },
  userRegist() {
    let loginId = this.data.registLoginId;
    let nickName = this.data.registNickName;
    let password = this.data.registPassword;
    let confirmPassword = this.data.confirmPassword;
    if (loginId == undefined || loginId == null || loginId == '') {
      wx.showToast({
        title: '请输入账号',
        icon: 'error'
      })
      return;
    }
    if (password == undefined || password == null || password == '') {
      wx.showToast({
        title: '请输入密码',
        icon: 'error'
      })
      return;
    }
    if (confirmPassword == undefined || confirmPassword == null || confirmPassword == '') {
      wx.showToast({
        title: '请输入密码',
        icon: 'error'
      })
      return;
    }
    if (password != confirmPassword) {
      wx.showToast({
        title: '两次输入密码不一样',
        icon: 'error'
      })
      return;
    }
    //先检查登陆号是否存在
    let obj = {
      method: 'GET',
      showLoading: false,
      url: 'user/checkLoginId?loginId=' + loginId
    }
    httpUtils.request(obj).then(res => {
      console.log(res.data.message)
      if (res.data.retCode == 0 && res.data.data) {
        ui.showToast('该登陆号已存在');
        return
      }
    }).catch(err => {
      ui.showToast('请求失败')
    });
    obj = {
      method: 'POST',
      showLoading: true,
      url: 'user/registerUser',
      data: {
        'loginId': loginId,
        'nickName': nickName,
        'password': password
      },
      message: '正在注册...'
    }
    httpUtils.request(obj).then(res => {
      console.log(res.data.message)
      if (res.data.retCode == 0 && res.data.data) {
        this.setData({
          current: 1,
          loginId: loginId,
          password: password,
          registLoginId: '',
          registNickName: '',
          registPassword: '',
          confirmPassword: ''
        })
      } else {
        ui.showToast(res.data.message)
      }
    }).catch(err => {
      console.log(err)
      ui.showToast('请求失败')
    });
  },

  touristLogin() {
    console.log('点击了游客登录');
    let obj = {
      method: 'POST',
      showLoading: true,
      url: 'auth/login',
      data: {
        'loginId': 'tourist',
        'password': '12345678'
      },
      message: '正在登录...'
    }
    httpUtils.request(obj).then(res => {
      console.log(res.data.message)
      if (res.data.retCode == 0) {
        wx.setStorageSync('token', res.data.data);
        wx.switchTab({
          url: '/pages/my-pages/home/home',
        })
      } else {
        ui.showToast(res.data.message)
      }
    }).catch(err => {
      ui.showToast('请求失败')
    });
  },

  /**
   * 用户登录
   */
  userLogin() {
    let loginId = this.data.loginId;
    let password = this.data.password;
    if (loginId == undefined || loginId == null || loginId == '') {
      wx.showToast({
        title: '请输入登录账号',
        icon: 'error'
      })
      return;
    }
    if (password == undefined || password == null || password == '') {
      wx.showToast({
        title: '请输入密码',
        icon: 'error'
      })
      return;
    }
    let obj = {
      method: 'POST',
      showLoading: true,
      url: 'auth/login',
      data: {
        'loginId': loginId,
        'password': password
      },
      message: '正在登录...'
    }
    httpUtils.request(obj).then(res => {
      console.log(res.data.message)
      if (res.data.retCode == 0) {
        wx.setStorageSync('token', res.data.data);
        this.setData({
          loginId: '',
          password: ''
        })
        wx.switchTab({
          url: '/pages/my-pages/home/home',
        })
      } else {
        ui.showToast(res.data.message)
      }
    }).catch(err => {
      ui.showToast('请求失败')
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})
